<template>
  <div class="box">
    <el-form :model="form" label-width="120px" v-loading="loading">
      <div style="display: inline-block">
        <el-form-item label="标题">
          <el-input v-model="form.title" style="width: 200px;" />
        </el-form-item>
        <el-form-item label="内容">
          <MdEditor v-model="form.content" noUploadImg />
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="onUpdate">修改</el-button>
        <el-button @click="quitEdit">退出</el-button>
      </el-form-item>
    </el-form>

    <div class="commentBox">
      <el-collapse>
        <el-collapse-item name="comment">
          <template #title>
            <el-icon size="20px">
              <Comment />
            </el-icon><span style="font-size: 20px;">评论管理</span>
          </template>
          <div>
            <commentListTable ref="tableRef"></commentListTable>
            <PageButton :total="pages" @pageChange="handleCurrentPageChange" ref="pageButton"></PageButton>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
import { reactive } from 'vue';
import { getArticleDetailById, updateArticle } from "@/api/article";
import { ElMessage } from 'element-plus';
import MdEditor from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';
import commentListTable from '@/components/backend/article/components/commentListTable.vue';
import PageButton from '@/components/backend/PageButton/PageButton.vue'

export default {
  name: "articleDetail",
  data() {
    return {
      // do not use same name with ref
      form: reactive({
        title: '',
        content: '',
      }),
      loading: false,
      //当前展示的id
      id: 0,
      currentPage: 1,
      pageSize: 10,
      pages: Number(10),
    }
  },
  components: {
    MdEditor,
    commentListTable,
    PageButton,
  },
  methods: {
    //提交修改
    onUpdate() {
      const data = this.form;
      return updateArticle(data)
        .then((response) => {
          this.quitEdit();
          this.$emit('updateSuccess');
          ElMessage.success("修改成功");
        })
        .catch((error) => {
          ElMessage.error("修改失败");
        });

    },

    quitEdit() {
      this.$emit('quitEdit');
    },

    //获取id并展示已有数据
    showArticle(id) {
      this.loading = true;
      this.id = id;
      const params = {
        id: this.id
      };
      return getArticleDetailById(params)
        .then((response) => {
          this.form = response.data.data;
          this.handleCurrentPageChange(1)
          setInterval(() => {
            this.loading = false;
          }, 1000);

        })
        .catch((error) => {
        });
    },
    handleCurrentPageChange(currentPage) {
      // 父组件处理currentPage变化的逻辑
      this.currentPage = currentPage;
      this.$nextTick(() => {
        this.$refs.tableRef.queryCommentList(this.pageSize, currentPage,this.id).then(() => {
          this.pages = this.$refs.tableRef.getPages();
          this.$refs.pageButton.setPageCount(this.pages);
          this.$refs.pageButton.setCurrentPage(this.currentPage);
        });
      });
    },
  },
}


</script>

<style scoped>
.box {
  width: 1200px;
  display: inline-block;
}
</style>